home *** CD-ROM | disk | FTP | other *** search
/ Acorn User: China / Acorn User China CD-ROM (UK) (Disc A) / Acorn User China CD-ROM (UK) (Disc A).bin / HENSA / TEXT / GNUDIFF.ARC / H_Diff < prev    next >
Encoding:
Text File  |  1991-09-28  |  11.1 KB  |  348 lines

  1. /* Shared definitions for GNU DIFF
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <limits.h>
  23. #include <ctype.h>
  24. #include <stdio.h>
  25. #include "sys/dir.h"
  26. #include "kernel.h"
  27. #include "swis.h"
  28. #include "sys/dir.h"
  29.   
  30. /* Utility functions */
  31. extern FILE *popen (char *, char *);
  32. extern int pclose (FILE *);
  33. extern int strucmp (const char *, const char *);
  34.  
  35. /* Define needed BSD functions in terms of ANSI library.  */
  36. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  37. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  38. #define bzero(s,n)    memset((s),0,(n))
  39. #define index        strchr
  40. #define rindex        strrchr
  41.  
  42. #define    EOS        (0)
  43. #define    FALSE        (0)
  44. #define TRUE        1
  45.  
  46. #define min(a,b) ((a) <= (b) ? (a) : (b))
  47. #define max(a,b) ((a) >= (b) ? (a) : (b))
  48.  
  49. #ifndef PR_FILE_NAME
  50. #define PR_FILE_NAME "Pr"
  51. #endif
  52.  
  53. /* Variables for command line options */
  54.  
  55. #ifndef GDIFF_MAIN
  56. #define EXTERN extern
  57. #else
  58. #define EXTERN
  59. #endif
  60.  
  61. enum output_style {
  62.   /* Default output style.  */
  63.   OUTPUT_NORMAL,
  64.   /* Output the differences with lines of context before and after (-c).  */
  65.   OUTPUT_CONTEXT,
  66.   /* Output the differences in a unified context diff format (-u). */
  67.   OUTPUT_UNIFIED,
  68.   /* Output the differences as commands suitable for `ed' (-e).  */
  69.   OUTPUT_ED,
  70.   /* Output the diff as a forward ed script (-f).  */
  71.   OUTPUT_FORWARD_ED,
  72.   /* Like -f, but output a count of changed lines in each "command" (-n). */
  73.   OUTPUT_RCS,
  74.   /* Output merged #ifdef'd file (-D).  */
  75.   OUTPUT_IFDEF };
  76.  
  77. /* True for output styles that are robust,
  78.    i.e. can handle a file that ends in a non-newline.  */
  79. #define ROBUST_OUTPUT_STYLE(S) \
  80.  ((S) == OUTPUT_CONTEXT || (S) == OUTPUT_UNIFIED || (S) == OUTPUT_RCS \
  81.   || (S) == OUTPUT_NORMAL)
  82.  
  83. EXTERN enum output_style output_style;
  84.  
  85. /* Number of lines of context to show in each set of diffs.
  86.    This is zero when context is not to be shown.  */
  87. EXTERN int      context;
  88.  
  89. /* Consider all files as text files (-a).
  90.    Don't interpret codes over 0177 as implying a "binary file".  */
  91. EXTERN int    always_text_flag;
  92.  
  93. /* Ignore changes in horizontal whitespace (-b).  */
  94. EXTERN int      ignore_space_change_flag;
  95.  
  96. /* Ignore all horizontal whitespace (-w).  */
  97. EXTERN int      ignore_all_space_flag;
  98.  
  99. /* Ignore changes that affect only blank lines (-B).  */
  100. EXTERN int      ignore_blank_lines_flag;
  101.  
  102. /* Ignore changes that affect only lines matching this regexp (-I).  */
  103. EXTERN char    *ignore_regexp;
  104.  
  105. /* Result of regex-compilation of `ignore_regexp'.  */
  106. EXTERN struct re_pattern_buffer ignore_regexp_compiled;
  107.  
  108. /* 1 if lines may match even if their lengths are different.
  109.    This depends on various options.  */
  110. EXTERN int      length_varies;
  111.  
  112. /* Ignore differences in case of letters (-i).  */
  113. EXTERN int      ignore_case_flag;
  114.  
  115. /* File labels for `-c' output headers (-L).  */
  116. EXTERN char *file_label[2];
  117.  
  118. /* Regexp to identify function-header lines (-F).  */
  119. EXTERN char    *function_regexp;
  120.  
  121. /* Result of regex-compilation of `function_regexp'.  */
  122. EXTERN struct re_pattern_buffer function_regexp_compiled;
  123.  
  124. /* Say only whether files differ, not how (-q).  */
  125. EXTERN int     no_details_flag;
  126.  
  127. /* Report files compared that match (-s).
  128.    Normally nothing is output when that happens.  */
  129. EXTERN int      print_file_same_flag;
  130.  
  131. /* character that ends a line.  Currently this is always `\n'.  */
  132. EXTERN char     line_end_char;
  133.  
  134. /* Output the differences with exactly 8 columns added to each line
  135.    so that any tabs in the text line up properly (-T).  */
  136. EXTERN int    tab_align_flag;
  137.  
  138. /* Expand tabs in the output so the text lines up properly
  139.    despite the characters added to the front of each line (-t).  */
  140. EXTERN int    tab_expand_flag;
  141.  
  142. /* In directory comparison, specify file to start with (-S).
  143.    All file names less than this name are ignored.  */
  144. EXTERN char    *dir_start_file;
  145.  
  146. /* If a file is new (appears in only one dir)
  147.    include its entire contents (-N).
  148.    Then `patch' would create the file with appropriate contents.  */
  149. EXTERN int    entire_new_file_flag;
  150.  
  151. /* Pipe each file's output through pr (-l).  */
  152. EXTERN int    paginate_flag;
  153.  
  154. /* String to use for #ifdef (-D).  */
  155. EXTERN char *    ifdef_string;
  156.  
  157. /* String containing all the command options diff received,
  158.    with spaces between and at the beginning but none at the end.
  159.    If there were no options given, this string is empty.  */
  160. EXTERN char *    switch_string;
  161.  
  162. /* Nonzero means use heuristics for better speed.  */
  163. EXTERN int    heuristic;
  164.  
  165. /* Name of program the user invoked (for error messages).  */
  166. EXTERN char *    program;
  167.  
  168. /* The result of comparison is an "edit script": a chain of `struct change'.
  169.    Each `struct change' represents one place where some lines are deleted
  170.    and some are inserted.
  171.    
  172.    LINE0 and LINE1 are the first affected lines in the two files (origin 0).
  173.    DELETED is the number of lines deleted here from file 0.
  174.    INSERTED is the number of lines inserted here in file 1.
  175.  
  176.    If DELETED is 0 then LINE0 is the number of the line before
  177.    which the insertion was done; vice versa for INSERTED and LINE1.  */
  178.  
  179. struct change
  180. {
  181.   struct change *link;        /* Previous or next edit command  */
  182.   int inserted;            /* # lines of file 1 changed here.  */
  183.   int deleted;            /* # lines of file 0 changed here.  */
  184.   int line0;            /* Line number of 1st deleted line.  */
  185.   int line1;            /* Line number of 1st inserted line.  */
  186.   char ignore;            /* Flag used in context.c */
  187. };
  188.  
  189. /* Structures that describe the input files.  */
  190.  
  191. /* Data on one line of text.  */
  192.  
  193. struct line_def {
  194.     char        *text;
  195.     int         length;
  196.     unsigned    hash;
  197. };
  198.  
  199. /* Time and date format conversion */
  200. #define TIMEFORM    "%W3 %M3 %DY %24:%MI:%SE %CE%YR\n"
  201.  
  202. /* Length of the converted string (including the terminating null) */
  203. #define TIMELEN        26
  204.  
  205. /* Data on one input file being compared.  */
  206.  
  207. struct file_data {
  208.     int             desc;        /* File descriptor */
  209.     char           *name;        /* File name */
  210.     unsigned int    length;        /* File length */
  211.     char            timestamp[TIMELEN];    /* File time stamp ("" if unstamped) */
  212.     int             dir_p;        /* 1 if file is a directory */
  213.  
  214.     /* Buffer in which text of file is read.  */
  215.     char *        buffer;
  216.     /* Allocated size of buffer.  */
  217.     int            bufsize;
  218.     /* Number of valid characters now in the buffer. */
  219.     int            buffered_chars;
  220.  
  221.     /* Array of data on analyzed lines of this chunk of this file.  */
  222.     struct line_def *linbuf;
  223.  
  224.     /* Allocated size of linbuf array (# of elements).  */
  225.     int            linbufsize;
  226.  
  227.     /* Number of elements of linbuf containing valid data. */
  228.     int            buffered_lines;
  229.  
  230.     /* Pointer to end of prefix of this file to ignore when hashing. */
  231.     char *prefix_end;
  232.  
  233.     /* Count of lines in the prefix. */
  234.     int prefix_lines;
  235.  
  236.     /* Pointer to start of suffix of this file to ignore when hashing. */
  237.     char *suffix_begin;
  238.  
  239.     /* Count of lines in the suffix. */
  240.     int suffix_lines;
  241.  
  242.     /* Vector, indexed by line number, containing an equivalence code for
  243.        each line.  It is this vector that is actually compared with that
  244.        of another file to generate differences. */
  245.     int           *equivs;
  246.  
  247.     /* Vector, like the previous one except that
  248.        the elements for discarded lines have been squeezed out.  */
  249.     int           *undiscarded;
  250.  
  251.     /* Vector mapping virtual line numbers (not counting discarded lines)
  252.        to real ones (counting those lines).  Both are origin-0.  */
  253.     int           *realindexes;
  254.  
  255.     /* Total number of nondiscarded lines. */
  256.     int            nondiscarded_lines;
  257.  
  258.     /* Vector, indexed by real origin-0 line number,
  259.        containing 1 for a line that is an insertion or a deletion.
  260.        The results of comparison are stored here.  */
  261.     char       *changed_flag;
  262.  
  263.     /* 1 if file ends in a line with no final newline. */
  264.     int            missing_newline;
  265.  
  266.     /* 1 more than the maximum equivalence value used for this or its
  267.        sibling file. */
  268.     int equiv_max;
  269.  
  270.     /* Table translating diff's internal line numbers 
  271.        to actual line numbers in the file.
  272.        This is needed only when some lines have been discarded.
  273.        The allocated size is always linbufsize
  274.        and the number of valid elements is buffered_lines.  */
  275.     int           *ltran;
  276. };
  277.  
  278. /* Describe the two files currently being compared.  */
  279.  
  280. EXTERN struct file_data files[2];
  281.  
  282. /* Queue up one-line messages to be printed at the end,
  283.    when -l is specified.  Each message is recorded with a `struct msg'.  */
  284.  
  285. struct msg
  286. {
  287.   struct msg *next;
  288.   char *format;
  289.   char *arg1;
  290.   char *arg2;
  291. };
  292.  
  293. /* Head of the chain of queues messages.  */
  294.  
  295. EXTERN struct msg *msg_chain;
  296.  
  297. /* Tail of the chain of queues messages.  */
  298.  
  299. EXTERN struct msg *msg_chain_end;
  300.  
  301. /* Stdio stream to output diffs to.  */
  302.  
  303. EXTERN FILE *outfile;
  304.  
  305. /* Declare various functions.  */
  306.  
  307. extern char *concat (char *, char *, char *);
  308.  
  309. extern int change_letter (int, int);
  310. extern int compare_files (char *, char *, char *, char *, int);
  311. extern int diff_2_files (struct file_data *, int);
  312. extern int diff_dirs (char *, char *, int (*)(), int, int, int);
  313. extern int line_cmp (struct line_def *, struct line_def *);
  314. extern int read_files (struct file_data *);
  315. extern int translate_line_number (struct file_data *, int);
  316.  
  317. extern struct change *find_change (struct change *);
  318. extern struct change *find_reverse_change (struct change *);
  319.  
  320. extern void *xmalloc (unsigned);
  321. extern void *xrealloc (void *, unsigned);
  322.  
  323. extern void analyze_hunk (struct change *, int *, int *, int *, int *, int *, int *);
  324. extern void debug_script (struct change *);
  325. extern void discard_confusing_lines (struct file_data *);
  326. extern void error (char *, ...);
  327. extern void fatal (char *);
  328. extern void find_and_hash_each_line (void);
  329. extern void finish_output (void);
  330. extern void message (char *, char *, char *);
  331. extern void perror_with_name (char *);
  332. extern void pfatal_with_name (char *);
  333. extern void pr_forward_ed_script (struct change *);
  334. extern void print_1_line (char *, struct line_def *);
  335. extern void print_context_header (struct file_data *, int);
  336. extern void print_context_script (struct change *, int);
  337. extern void print_ed_script (struct change *);
  338. extern void print_ifdef_script (struct change *);
  339. extern void print_message_queue (void);
  340. extern void print_normal_hunk (struct change *);
  341. extern void print_normal_script (struct change *);
  342. extern void print_number_range (char, struct file_data *, int, int);
  343. extern void print_rcs_script (struct change *);
  344. extern void print_script (struct change *, struct change *(*)(), void (*)());
  345. extern void setup_output (char *, char *, int);
  346. extern void specify_style (enum output_style);
  347. extern void translate_range (struct file_data *, int, int, int *, int *);
  348.